package com.qdlc.p2p.biz.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.EnjoyBorrowService;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.EnjoyBorrowDao;
import com.qdlc.p2p.dal.dao.EnjoyUploadDao;
import com.qdlc.p2p.dal.dao.VerifyLogDao;
import com.qdlc.p2p.dal.dto.EnjoyBorrow;
import com.qdlc.p2p.dal.dto.VerifyLog;
import com.qdlc.p2p.dal.model.EnjoyBorrowModel;

/**
 * Created by D on 2016/1/20.
 */
@Service("enjoyBorrowService")
public class EnjoyBorrowServiceImpl implements EnjoyBorrowService {

    @Resource
    private EnjoyBorrowDao enjoyBorrowDao;

    @Resource
    private EnjoyUploadDao enjoyUploadDao;

    @Override
    public void save(EnjoyBorrow enjoyBorrow) {
        enjoyBorrowDao.save(enjoyBorrow);
    }

    @Override
    public void update(EnjoyBorrow enjoyBorrow) {
        enjoyBorrowDao.update(enjoyBorrow);
    }

    @Override
    public EnjoyBorrow findByBorrowId(long borrowId) {
        return enjoyBorrowDao.findByProperty("borrow.id", borrowId).get(0);
    }

    @Override
    public PageDataList<EnjoyBorrowModel> getList(int prefecture, int page) {
        PageDataList<EnjoyBorrow> pageDataList = enjoyBorrowDao.getList(prefecture, page);
        PageDataList<EnjoyBorrowModel> pageDataList_ = new PageDataList<EnjoyBorrowModel>();
        List<EnjoyBorrowModel> list = new ArrayList<EnjoyBorrowModel>();
        pageDataList_.setPage(pageDataList.getPage());
        VerifyLogDao verifyLogDao = (VerifyLogDao) BeanUtil.getBean("verifyLogDao");

        if (pageDataList.getList().size() > 0) {
            for (int i = 0; i < pageDataList.getList().size(); i++) {
                EnjoyBorrow enjoyborrow = pageDataList.getList().get(i);
                EnjoyBorrowModel eb = EnjoyBorrowModel.instance(enjoyborrow);

                VerifyLog log = verifyLogDao.findByType(enjoyborrow.getBorrow().getId(), "borrow", 1);
                if (log != null) {
                    eb.setStartTime(DateUtil.dateStr4(log.getTime()));
                }
                eb.setName(enjoyborrow.getEnjoyGoods().getName());
                eb.setUuid(enjoyborrow.getBorrow().getUuid());
                eb.setLowestAccount(enjoyborrow.getBorrow().getLowestAccount());
                eb.setPrice(eb.getEnjoyGoods().getPrice());
                eb.setTimeLimit(enjoyborrow.getBorrow().getTimeLimit());
                eb.setTimeType(enjoyborrow.getBorrow().getBorrowTimeType());
                eb.setPicPath((enjoyUploadDao.findPicByIdAndType(eb.getEnjoyGoods().getId(), 1)).get(0) + "");
                double apr = BigDecimalUtil.div(enjoyborrow.getBorrow().getApr(), enjoyborrow.getBorrow().getBorrowTimeType() == 1 ? 36500 : 1200);
                double aprAccount = BigDecimalUtil.round(BigDecimalUtil.mul(enjoyborrow.getBorrow().getLowestAccount(), apr, enjoyborrow.getBorrow().getTimeLimit()));
                eb.setAprAccount(aprAccount);
                eb.setPosition(enjoyborrow.getEnjoyGoods().getPosition());
                eb.setContent(enjoyborrow.getEnjoyGoods().getContent());
                list.add(eb);
            }
        }
        pageDataList_.setList(list);
        return pageDataList_;
    }
}
